package top.likeqc.leetcode.editor.cn;

/**
 * @see <a href="https://leetcode.cn/problems/decode-the-message">2325. 解密消息</a>
 */
public class DecodeTheMessage {
    public static void main(String[] args) {
        Solution solution = new DecodeTheMessage().new Solution();
        System.out.println(
                solution.decodeMessage(
                        "the quick brown fox jumps over the lazy dog", "vkbs bs t suepuv"));

        System.out.println(
                solution.decodeMessage(
                        "eljuxhpwnyrdgtqkviszcfmabo", "zwx hnfx lqantp mnoeius ycgk vcnjrdb"));
    }

    // leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public String decodeMessage(String key, String message) {
            int count = 0;
            char[] cc = new char[26];

            for (char c : key.toCharArray()) {
                if (c >= 'a' && c <= 'z' && cc[c - 'a'] == 0) {
                    cc[c - 'a'] = (char) ('a' + count++);
                }
            }

            StringBuffer sb = new StringBuffer();
            for (char c : message.toCharArray()) {
                if (c >= 'a' && c <= 'z') {
                    sb.append(cc[c - 'a']);
                } else {
                    sb.append(c);
                }
            }

            return sb.toString();
        }
    }
    // leetcode submit region end(Prohibit modification and deletion)

}
